<?php
namespace app\index\controller;
use app\index\controller\Base;
use think\Session;
class Release extends Base{
    // 用户发布文章
    public function index(){
    	if(!Session::get('username')){
    		$this->success('请先登陆！','Login/index');
    	}else{
    		return $this->fetch('release');
    	}       
    }   
    public function release(){  
        $author=Session::get('username');
        $username=Session::get('username');
        $user=db('user')->where('username',$username)->column('id');       
        $userid=$user[0];
        if(request()->isPost()){
            // dump($_POST); die;
			$data=[
    			'title'=>input('title'),
                'author'=>$author,
                'desc'=>input('desc'),
                'keywords'=>str_replace('，', ',', input('keywords')),//关键字中英文逗号转换
                'content'=>input('content'),
                // 'cateid'=>input('cateid'),                
                'time'=>time(),
                'userid'=>$userid,          
            ];
            if(input('state')=='on'){
                $data['state']=1;
            }//是否推荐
            if($_FILES['pic']['tmp_name']){
                $file = request()->file('pic');
                $info = $file->move(ROOT_PATH . 'public' . DS . 'static/uploads');
                $data['pic']='/uploads/'.$info->getSaveName();
            }//是否上传照片
    		if(db('Produce')->insert($data)){
    			return $this->success('发布文章成功！','Blog/index');
    		}else{
    			return $this->error('发布文章失败！');
    		} 
    		return;
        }
        $this->assign('users',$users);
    	return $this->fetch();
        // $cateres=db('cate')->select();
        // $this->assign('cateres',$cateres);
        // return $this->fetch();
    }
}
